home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / program / cgrphxdv.lha / CGraphX / C / Examples / Window.c < prev   
C/C++ Source or Header  |  1995-12-18  |  10KB  |  404 lines

  1.  
  2. #include <exec/memory.h>
  3. #include <intuition/intuitionbase.h>
  4. #include <cybergraphics/cybergraphics.h>
  5.  
  6. #ifdef __SASC_60
  7. #include <proto/cybergraphics.h>
  8. #include <proto/dos.h>
  9. #include <proto/exec.h>
  10. #include <proto/intuition.h>
  11. #else
  12. #include <clib/cybergraphics_protos.h>
  13. #include <clib/dos_protos.h>
  14. #include <clib/exec_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #ifndef _DCC
  17. #include <inline/cybergraphics.h>
  18. #endif
  19. #endif
  20.  
  21. #include <stdlib.h>
  22.  
  23. #define CYBERGFXNAME     "cybergraphics.library"
  24. #define CYBERGFXVERSION  40L
  25.  
  26. #define SCREEN_WANTED_WIDTH  640L
  27. #define SCREEN_WANTED_HEIGHT 480L
  28. #define SCREEN_WANTED_DEPTH   24L
  29. #define SCREEN_MIN_DEPTH      15L
  30.  
  31. #define IMAGE_WIDTH  256L
  32. #define IMAGE_HEIGHT 256L
  33. #define IMAGE_BPP    3L
  34.  
  35. #ifdef __SASC_60
  36. #include <string.h>
  37. #define MIN(a,b) __builtin_min(a,b)
  38. #else
  39. #define MIN(a,b) ((a)<(b)?(a):(b))
  40. #endif
  41. #define SIGMASK(w) (1L<<((w)->UserPort->mp_SigBit))
  42. #define GETIMSG(w) ((struct IntuiMessage *)GetMsg((w)->UserPort))
  43.  
  44. #ifdef __SASC_650
  45. /* use SAS/C 6.5x's autoinit feature */
  46.  
  47. LONG __oslibversion   = 39L;
  48. LONG __CGFXlibversion = CYBERGFXVERSION;
  49. #else
  50. struct IntuitionBase *IntuitionBase;
  51. struct Library *CyberGfxBase;
  52. #endif
  53.  
  54. #ifdef __SASC_650
  55. char Version[] = "$VER: Window 0.4 " __AMIGADATE__ ;
  56. #else
  57. #ifdef _DCC
  58. char Version[] = "$VER: Window 0.4 (" __COMMODORE_DATE__  ")";
  59. #else
  60. char Version[] = "$VER: Window 0.4 (" __DATE__  ")";
  61. #endif
  62. #endif
  63.  
  64. /*
  65.  
  66.         display error message
  67.  
  68. */
  69.  
  70. void ErrorF(char *Format,...)
  71.  
  72. {
  73.  void *Data;
  74.  
  75.  Data=&Format+1L;
  76.  if (((struct Process *)FindTask(NULL))->pr_CLI)
  77.   {
  78.    (void)VPrintf(Format,Data);
  79.    (void)FPutC(Output(),'\n');
  80.    (void)Flush(Output());
  81.   }
  82.  else
  83.   {
  84.    struct EasyStruct EasyStruct;
  85.    ULONG IDCMPFlags;
  86.  
  87.    EasyStruct.es_StructSize=sizeof(struct EasyStruct);
  88.    EasyStruct.es_Flags=0L;
  89.    EasyStruct.es_Title="Error";
  90.    EasyStruct.es_TextFormat=Format;
  91.    EasyStruct.es_GadgetFormat="Ok";
  92.  
  93.    IDCMPFlags=0L;
  94.    (void)EasyRequestArgs(NULL,&EasyStruct,&IDCMPFlags,Data);
  95.   }
  96. }
  97.  
  98. /*
  99.  
  100.         create image data
  101.  
  102. */
  103.  
  104. void CreateImageData(UBYTE *Image,ULONG Width,ULONG Height)
  105.  
  106. {
  107.  ULONG X,Y;
  108.  
  109.  for (Y=0; Y<Height; Y++)
  110.   for (X=0; X<Width; X++)
  111.    {
  112.     *Image++=(UBYTE)X; /* Red   */
  113.     *Image++=(UBYTE)Y; /* Green */
  114.     *Image++=0;        /* Blue  */
  115.    }
  116. }
  117.  
  118. /*
  119.  
  120.         window handling
  121.  
  122. */
  123.  
  124. LONG InnerWidth(struct Window *Window)
  125.  
  126. {
  127.  return Window->Width-Window->BorderLeft-Window->BorderRight;
  128. }
  129.  
  130. LONG InnerHeight(struct Window *Window)
  131.  
  132. {
  133.  return Window->Height-Window->BorderTop-Window->BorderBottom;
  134. }
  135.  
  136. void RedrawScaleWindow(struct Window *ScaleWindow,UBYTE *ImageData)
  137.  
  138. {
  139.  (void)ScalePixelArray(ImageData,IMAGE_WIDTH,IMAGE_HEIGHT,
  140.                        IMAGE_WIDTH*IMAGE_BPP,
  141.                        ScaleWindow->RPort,
  142.                        ScaleWindow->BorderLeft,ScaleWindow->BorderTop,
  143.                        InnerWidth(ScaleWindow),
  144.                        InnerHeight(ScaleWindow),
  145.                        RECTFMT_RGB);
  146. }
  147.  
  148. void RedrawWriteWindow(struct Window *WriteWindow,UBYTE *ImageData)
  149.  
  150. {
  151.  (void)WritePixelArray(ImageData,0,0,
  152.                        IMAGE_WIDTH*IMAGE_BPP,
  153.                        WriteWindow->RPort,
  154.                        WriteWindow->BorderLeft,WriteWindow->BorderTop,
  155.                        InnerWidth(WriteWindow),
  156.                        InnerHeight(WriteWindow),
  157.                        RECTFMT_RGB);
  158. }
  159.  
  160. /*
  161.  
  162.         screen depth fallback
  163.  
  164. */
  165.  
  166. ULONG NextDepth(ULONG Depth)
  167.  
  168. {
  169.  switch (Depth)
  170.   {
  171.    case 24L: /* 24Bit not available? Try 16Bit!      */
  172.     return 16L;
  173.    case 16L: /* 15Bit not available? Try 15Bit!      */
  174.     return 15L;
  175.    default:  /* Not even 15Bit available? Forget it! */
  176.     return 0L;
  177.   }
  178. }
  179.  
  180. /*
  181.  
  182.         main program
  183.  
  184. */
  185.  
  186. int main(int argc, char **argv)
  187.  
  188. {
  189.  ULONG DisplayID,Depth;
  190.  struct Screen *CyberScreen;
  191.  struct Window *ScaleWindow,*WriteWindow;
  192.  UBYTE *ImageData;
  193.  LONG Done;
  194.  struct IntuiMessage *IntMsg;
  195.  
  196. #ifndef __SASC_650
  197.  if ((IntuitionBase=(struct IntuitionBase *)
  198.                     OpenLibrary("intuition.library",39L))==NULL) return 20;
  199.  
  200.  if ((CyberGfxBase=OpenLibrary(CYBERGFXNAME,CYBERGFXVERSION))==NULL)
  201.   {
  202.    CloseLibrary (&IntuitionBase->LibNode);
  203.  
  204.    ErrorF ("Can't open \"%s\" version %ld or newer.",
  205.            CYBERGFXNAME,CYBERGFXVERSION);
  206.    return 10;
  207.   }
  208. #endif
  209.  
  210. /* Let CyberGraphX search a display mode for us! */
  211.  
  212.  Depth=SCREEN_WANTED_DEPTH;
  213.  while (Depth)
  214.   {
  215.    if ((DisplayID=BestCModeIDTags(CYBRBIDTG_NominalWidth,SCREEN_WANTED_WIDTH,
  216.                                   CYBRBIDTG_NominalHeight,SCREEN_WANTED_HEIGHT,
  217.                                   CYBRBIDTG_Depth,Depth,
  218.                                   TAG_DONE))!=INVALID_ID)
  219.     {
  220. /* Because older version of the "cybergraphics.library" don't handle */
  221. /* CYBRBIDTG_Depth properly we query the real depth of the mode.     */
  222.  
  223.      Depth=GetCyberIDAttr(CYBRIDATTR_DEPTH,DisplayID);
  224.      break;
  225.     }
  226. /* retry with less bits per pixel */
  227.    Depth=NextDepth(Depth);
  228.   }
  229.  
  230.  if (Depth<SCREEN_MIN_DEPTH)
  231.   {
  232. #ifndef __SASC_650
  233.    CloseLibrary (CyberGfxBase);
  234.    CloseLibrary (&IntuitionBase->LibNode);
  235. #endif
  236.  
  237.    ErrorF ("Can't find suitable display mode for %ldx%ldx%ld.",
  238.            SCREEN_WANTED_WIDTH,SCREEN_WANTED_HEIGHT,SCREEN_WANTED_DEPTH);
  239.    return 5;
  240.   }
  241.  
  242. /* open screen, but let Intuition choose the actual dimensions */
  243.  
  244.  if ((CyberScreen=OpenScreenTags(NULL,
  245.                                  SA_Title,"CyberGraphX Demo",
  246.                                  SA_DisplayID,DisplayID,
  247.                                  SA_Depth,Depth,
  248.                                  TAG_DONE))==NULL)
  249.   {
  250. #ifndef __SASC_650
  251.    CloseLibrary (CyberGfxBase);
  252.    CloseLibrary (&IntuitionBase->LibNode);
  253. #endif
  254.  
  255.    ErrorF ("Can't open screen.");
  256.    return 5;
  257.   }
  258.  
  259. /* create Scale window */
  260.  
  261.  if ((ScaleWindow=OpenWindowTags(NULL,
  262.                                  WA_Title,"Scale",
  263.                                  WA_Flags,WFLG_ACTIVATE|WFLG_SIMPLE_REFRESH|
  264.                                   WFLG_SIZEGADGET|WFLG_RMBTRAP|WFLG_DRAGBAR|
  265.                                   WFLG_DEPTHGADGET|WFLG_CLOSEGADGET,
  266.                                  WA_IDCMP,
  267.                                   IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|
  268.                                   IDCMP_SIZEVERIFY|IDCMP_NEWSIZE,
  269.                                  WA_Left,16,
  270.                                  WA_Top,CyberScreen->BarHeight+16,
  271.                                  WA_Width,IMAGE_WIDTH,
  272.                                  WA_Height,IMAGE_HEIGHT,
  273.                                  WA_CustomScreen,CyberScreen,
  274.                                  TAG_DONE))==NULL)
  275.   {
  276.    CloseScreen (CyberScreen);
  277. #ifndef __SASC_650
  278.    CloseLibrary (CyberGfxBase);
  279.    CloseLibrary (&IntuitionBase->LibNode);
  280. #endif
  281.  
  282.    ErrorF ("Can't open scale window.");
  283.    return 5;
  284.   }
  285.  (void)WindowLimits(ScaleWindow,
  286.                     ScaleWindow->BorderLeft+ScaleWindow->BorderRight+1,
  287.                     ScaleWindow->BorderTop+ScaleWindow->BorderBottom+1,
  288.                     CyberScreen->Width,CyberScreen->Height);
  289.  
  290. /* create Write window */
  291.  
  292.  if ((WriteWindow=OpenWindowTags(NULL,
  293.                                  WA_Title,"Write",
  294.                                  WA_Flags,WFLG_SIMPLE_REFRESH|
  295.                                   WFLG_SIZEGADGET|WFLG_RMBTRAP|WFLG_DRAGBAR|
  296.                                   WFLG_DEPTHGADGET|WFLG_CLOSEGADGET,
  297.                                  WA_IDCMP,
  298.                                   IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|
  299.                                   IDCMP_SIZEVERIFY|IDCMP_NEWSIZE,
  300.                                  WA_Left,CyberScreen->Width-16-IMAGE_WIDTH,
  301.                                  WA_Top,CyberScreen->BarHeight+16,
  302.                                  WA_Width,IMAGE_WIDTH,
  303.                                  WA_Height,IMAGE_HEIGHT,
  304.                                  WA_CustomScreen,CyberScreen,
  305.                                  TAG_DONE))==NULL)
  306.   {
  307.    CloseWindow (ScaleWindow);
  308.    CloseScreen (CyberScreen);
  309. #ifndef __SASC_650
  310.    CloseLibrary (CyberGfxBase);
  311.    CloseLibrary (&IntuitionBase->LibNode);
  312. #endif
  313.  
  314.    ErrorF ("Can't open write window.");
  315.    return 5;
  316.   }
  317.  (void)WindowLimits(WriteWindow,
  318.                     WriteWindow->BorderLeft+WriteWindow->BorderRight+1,
  319.                     WriteWindow->BorderTop+WriteWindow->BorderBottom+1,
  320.                     MIN(CyberScreen->Width,WriteWindow->BorderLeft+
  321.                         WriteWindow->BorderRight+IMAGE_WIDTH),
  322.                     MIN(CyberScreen->Height,WriteWindow->BorderTop+
  323.                         WriteWindow->BorderBottom+IMAGE_HEIGHT));
  324.  
  325. /* allocate and create image data */
  326.  
  327.  if ((ImageData=AllocVec(IMAGE_WIDTH*IMAGE_HEIGHT*IMAGE_BPP,MEMF_PUBLIC))==NULL)
  328.   {
  329.    CloseWindow (WriteWindow);
  330.    CloseWindow (ScaleWindow);
  331.    CloseScreen (CyberScreen);
  332. #ifndef __SASC_650
  333.    CloseLibrary (CyberGfxBase);
  334.    CloseLibrary (&IntuitionBase->LibNode);
  335. #endif
  336.  
  337.    ErrorF ("Out of memory.");
  338.    return 5;
  339.   }
  340.  CreateImageData (ImageData,IMAGE_WIDTH,IMAGE_HEIGHT);
  341.  
  342. /* event loop */
  343.  
  344.  RedrawScaleWindow (ScaleWindow,ImageData);
  345.  RedrawWriteWindow (WriteWindow,ImageData);
  346.  
  347.  Done=FALSE;
  348.  while (!Done)
  349.   {
  350.    (void)Wait(SIGMASK(WriteWindow)|SIGMASK(ScaleWindow));
  351.  
  352.    while (IntMsg=GETIMSG(ScaleWindow))
  353.     {
  354.      switch (IntMsg->Class)
  355.       {
  356.        case IDCMP_REFRESHWINDOW:
  357.         BeginRefresh (ScaleWindow);
  358.         RedrawScaleWindow (ScaleWindow,ImageData);
  359.         EndRefresh (ScaleWindow,TRUE);
  360.         break;
  361.        case IDCMP_NEWSIZE:
  362.         RedrawScaleWindow (ScaleWindow,ImageData);
  363.         break;
  364.        case IDCMP_CLOSEWINDOW:
  365.         Done=TRUE;
  366.       }
  367.  
  368.      ReplyMsg (&IntMsg->ExecMessage);
  369.     }
  370.  
  371.    while (IntMsg=GETIMSG(WriteWindow))
  372.     {
  373.      switch (IntMsg->Class)
  374.       {
  375.        case IDCMP_REFRESHWINDOW:
  376.         BeginRefresh (WriteWindow);
  377.         RedrawWriteWindow (WriteWindow,ImageData);
  378.         EndRefresh (WriteWindow,TRUE);
  379.         break;
  380.        case IDCMP_NEWSIZE:
  381.         RedrawWriteWindow (WriteWindow,ImageData);
  382.         break;
  383.        case IDCMP_CLOSEWINDOW:
  384.         Done=TRUE;
  385.       }
  386.  
  387.      ReplyMsg (&IntMsg->ExecMessage);
  388.     }
  389.   }
  390.  
  391. /* cleanup */
  392.  
  393.  FreeVec (ImageData);
  394.  CloseWindow (WriteWindow);
  395.  CloseWindow (ScaleWindow);
  396.  CloseScreen (CyberScreen);
  397. #ifndef __SASC_650
  398.  CloseLibrary (CyberGfxBase);
  399.  CloseLibrary (&IntuitionBase->LibNode);
  400. #endif
  401.  
  402.  return 0;
  403. }
  404.